<template>
  <el-row class="login-container">
    <el-col :lg="16" :md="12" class="left"><!-- 垂直水平方向居中 -->
      <div>
        <div class="l_box01">欢迎光临</div>
        <div class="l_box02">此处是副标题</div>
      </div>
    </el-col>
    <el-col :lg="8" :md="12" class="right">
      <h2>欢迎登录</h2>
      <div class="r_box01">
        <span class="span"></span>
        <span>账号密码登录</span>
        <span class="span"></span>
      </div>
      <el-form ref="formRef" :rules="rules" :model="form" class="w-[250px]">
        <el-form-item label="账号：" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input type="password" show-password v-model="form.password" placeholder="请输入密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
      
        <el-form-item>
          <el-button class="w-[250px]" round color="#626aef" type="primary" @click="onSubmit" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script  setup>
import { reactive,ref,onMounted,onBeforeMount } from 'vue' //onMounted 页面进入的生命周期函数   onBeforeMount  页面卸载的生命周期函数
import { login } from '~/api/manager'
//import { ElMessage } from 'element-plus'
import { toast } from '~/composables/util'
import { useRouter } from 'vue-router';
import { useStore } from 'vuex'
//import { useCookies } from '@vueuse/integrations/useCookies';
//import { setToken } from '~/composables/auth'

const router = useRouter()
const store = useStore()

// do not use same name with ref
const form = reactive({
  username: '',
  password: '',

})

const rules = {
  username : [  //验证规则
    { required: true, message: '用户名不能为空', trigger: 'blur' },//required必填，message验证提示，trigger触发时机--blur失去焦点
    //{ min: 3, max: 5, message: '用户名在3-5个字符内', trigger: 'blur' },
  ],
  password : [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    //{ min: 3, max: 12, message: '密码在3-12个字符内', trigger: 'blur' },
  ],
}

//变成响应式
const formRef = ref(null) 
const loading = ref(false)
const onSubmit = () => {
  formRef.value.validate((valid)=>{
    //console.log(valid)
    if(!valid){
      return false
    }

    loading.value = true
    //console.log('验证通过')


    store.dispatch("login",form).then(res=>{
      toast("登录成功")
      router.push("/")
    }).finally(()=>{
        loading.value=false
    })
    /* login(form.username,form.password)
    .then(res=>{
      console.log(res)

      //提示成功
      toast("登陆成功")
      ElMessage({
        message: "登陆成功",
        type: 'success',
        //plain: true,
        duration:3000,
      })

      //存储token
      //const cookie = useCookies()
      //cookie.set("admin-token",res.token)
      setToken(res.token)

      //获取用户相关信息
      getinfo().then(res2=>{
        store.commit("SET_USERINFIFO",res2)
        console.log(res2)
      })

      //跳转到后台首页
      router.push("/")
    }).finally(()=>{
        loading.value=false
    }) */
  })
}

//监听回车事件
function onKeyUp(e){
  //console.log(e);
  if(e.key=="Enter") onSubmit()
}

//添加键盘监听
onMounted(()=>{
  document.addEventListener("keyup",onKeyUp)
})
//移除键盘监听
onBeforeMount(()=>{
  document.removeEventListener("keyup",onKeyUp)
})


</script>

<style lang='scss'>
.login-container {
  @apply bg-indigo-500 min-h-screen;
  .left {
    @apply flex items-center justify-center;
    .l_box01 {
      @apply font-bold text-5xl text-light-50 mb-4;
    }
    .l_box02 {
      @apply text-gray-200 text-sm;
    }
  }
  .right {
    @apply bg-light-50 flex items-center justify-center flex-col;
    h2 {
      @apply font-bold text-3xl text-gray-800;
    }
    .r_box01 {
      @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
      .span {
        @apply h-[1px] w-16 bg-gray-200;
      }
    }
  }
}
</style>